
var app = getApp();

Page({
    data: {
       
    },
    onLoad: function (options) {
      var userInfo = wx.getStorageSync('userInfo');
      var useronlyData = wx.getStorageSync('useronlyData');
      this.setData({
        userInfo: userInfo,
        useronlyData: useronlyData
      })
    },
    getMyInfo:function(){
      var that = this ;
      var openid = that.data.useronlyData.openid;
      wx.navigateTo({
        url: '../../idCard/my/myInfo?openid=' + openid,
        success: function (res) {
          // success

        },
        fail: function () {
          // fail

        },
        complete: function () {
          // complete
        }
      })
    }
    ,
    createUser:function(){
      var that = this;
      var wxName = that.data.userInfo.nickName;
      var openid = that.data.useronlyData.openid;
      if (openid==undefined || wxName==undefined){
           wx.showToast({
             title: '请先登录微信!!!',
             icon :'none',
             duration: 3000
           })
      }else{
        wx.request({
          url: 'http://localhost:8080/isExistUser2?openId=' + openid,
          method: 'GET',
          success: function (res) {
            var isExist = res.data;
            if (!isExist) {
              wx.navigateTo({
                url: '../../idCard/updateCard/updateCard?openid=' + openid + '&wxName=' + wxName,
              })
            } else {
              wx.showToast({
                title: '您已经成为会员',
                icon: 'none',
                duration: 2000
              })
            }

          }
        })
      }
  }, createUserCard: function () {
    var that = this
    var openid = that.data.useronlyData.openid;
    console.log(openid)
    if( openid == undefined){
      wx.showToast({
        title: '请先成为会员!!!',
        icon: 'none',
        duration: 3000
      })
    }else{
      wx.navigateTo({
        url: '../../idCard/createCard/createCard?openid=' + openid
      })
    }
  },
  help:function(){
    var openid = wx.getStorageSync('useronlyData').openid;
    if(openid == undefined){
         wx.showToast({
           title: '请先登录微信!!!',
           icon: 'none',
           duration: 3000
         })
    }else{
      wx.navigateTo({
        url: '../../help/help_each_other/help_each_other'
      })
    }
  }
  ,
  MyStar:function(){
    var that = this
    var openId =wx.getStorageSync('useronlyData').openid
    if (openId == undefined){
       wx.showToast({
         title: '请先成为会员!!!',
         icon: "none",
         duration : 3000
        
       })
    }else{
      wx.navigateTo({
        url: '../../posts/post-star/post-star'
      })
    }
  }
    ,
    onReady: function () {

    },
    onShow: function () {


    },
    onHide: function () {
        // 页面隐藏

    },
    onUnload: function () {
        // 页面关闭
    },
  bindGetUserInfo(e) {
    var that = this;
    
    var useronlyData = wx.getStorageSync('useronlyData');
    
    if (wx.getStorageSync('userInfo') == '') {
      
      wx.login({
        success: function (res) {
          var code = res.code;//登录凭证
          if (code) {
            wx.getUserInfo({
              success: function (res) {
                var userInfo2 = res.userInfo //用户基本信息
                wx.setStorageSync('userInfo', userInfo2)
                app.globalData.userInfo = userInfo2
                that.setData({
                  userInfo: userInfo2
                  })
                wx.request({
                  url: 'http://localhost:8080/login/login_weixin',//自己的服务接口地址
                  method: 'GET',
                  header: {
                    "Content-Type": "applciation/json"
                  },
                  data: { code: code },
                  success: function (res) {
                    var data = res.data;
                    wx.setStorageSync('useronlyData', data)
                    that.setData({
                      useronlyData: data
                    })
                  },
                  fail: function () {
                    console.log('系统错误')
                  }
                })
                }
              })
          } else {
            console.log('获取用户登录态失败！' + r.errMsg)
          }
        },
        fail: function () {
          console.log('登陆失败')
        }
      }) 
    } else {
      wx.showToast({
        title: '您已经登陆了',
        icon: 'none',
        duration: 2000
      })
    }
  }
    
    
})